<?php
class DatePickerWidget extends sfWidgetFormInput {
	protected function configure($options = array(), $attributes = array()) {
		// $this->setAttribute('rich',                isset($attributes['rich'])?                $attributes['rich']                : 'true');
		// $this->setAttribute('readonly',            isset($attributes['readonly'])?            $attributes['readonly']            : 'readonly');
		$this->setAttribute('culture',             isset($attributes['culture'])?             $attributes['culture']             : 'fr');
		$this->setAttribute('format',              isset($attributes['format'])?              $attributes['format']              : 'dd/mm/YYYY');
		// $this->setAttribute('calendar_button_img', isset($attributes['calendar_button_img'])? $attributes['calendar_button_img'] : '/images/common/calendar.png');
		parent::configure($options, $attributes);
	}

	public function render($name, $value = null, $attributes = array(), $errors = array()) {
		if (preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $value, $matches) == 1) {
			$yyyy = $matches[1];
			$mm = $matches[2];
			$dd = $matches[3];
			switch ($this->getAttribute('format')) {
				case 'dd/mm/YYYY':
					$value = "$dd/$mm/$yyyy";
					break;
			}
		}
		return $this->renderTag('input', array_merge(array('type' => $this->getOption('type'), 'name' => $name, 'value' => $value), $attributes));
	}
}
?>
